<template>
  <div class="login_component">
    <div class="login_box">
      <div style="flex: 4; display: flex; align-items: center; justify-content: center">
        <img src="@/assets/logo.png" alt="">
      </div>
      <div style="flex: 6; display: flex; align-items: center; justify-content: center">

        <el-form :model="user" style="width: 80%" :rules="rules" ref="loginRef">
          <div style="display: flex; align-items: center; justify-content: center; margin-bottom: 10px">
            <span style="font-size: 20px; text-align: center">浙江省清廉学校建设评估系统</span>
          </div>

          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号"
                      v-model="user.username"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>

          <el-form-item prop="code">
            <div style="display: flex">
              <el-input prefix-icon="el-icon-circle-check" size="medium" style="flex: 1"
                        placeholder="请输入验证码" v-model="user.code"></el-input>
              <div style="flex: 1; height: 36px">
                <ValidCode @update:value="getCode"/>
              </div>
            </div>
          </el-form-item>

          <el-form-item>
            <el-button class="login_button" type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
    <el-footer style="position: absolute;bottom: 0;left: 980px">
      本系统使用的vue图标来自项目创建自带，仅作为测试使用，如有侵权请联系删除
    </el-footer>
  </div>
</template>

<script>
import ValidCode from "@/conpontents/ValidCode.vue";
import request from "@/urils/request";

export default {
  name: "Login",
  components: {
    ValidCode
  },
  data() {
    const codeJudge = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('验证码不能为空'));
      } else if (value.toLowerCase() !== this.code) {
        callback(new Error('验证码错误'));
      } else {
        callback();
      }
    };
    return {
      code: "",
      user: {
        username: "",
        password: "",
        code: "",
      },
      rules: {
        username: [
          {required: true, message: "账号不能为空", trigger: "blur"}
        ],
        password: [
          {required: true, message: "密码不能为空", trigger: "blur"}
        ],
        code: [
          {validator: codeJudge, trigger: "blur"}
        ]
      }
    }
  },
  methods: {
    getCode(code) {
      this.code = code.toLowerCase()
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          request.post("/user/login", this.user).then(res => {
            if (res.code === '200') {
              this.$message.success("登录成功")
              localStorage.setItem("user", JSON.stringify(res.data))
              this.$router.push('/')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
  }
}
</script>

<style scoped>
.login_component {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #40a99f;
  position: relative;
}

.login_box {
  display: flex;
  background-color: white;
  width: 50%;
  height: 50%;
  overflow: hidden;
  border-radius: 5px;
}

.login_button {
  width: 100%;
  background-color: #40a99f !important;
}


</style>